package de.baumann.browser.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import de.baumann.browser.OdinGlideModule;
import de.baumann.browser.R;
import de.baumann.browser.activitys.ReadArticleActivity;
import de.baumann.browser.api.net.vo.NewsItem;
import de.baumann.browser.utils.TimeUtil;
import de.baumann.browser.utils.UserDataKt;

public class NewsItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<NewsItem> mNewsList;

    public NewsItemAdapter(Context context) {
        mContext = context;
    }

    public void setNewsList(ArrayList<NewsItem> mNewsList) {
        this.mNewsList = mNewsList;
        notifyDataSetChanged();
    }

    public void addNewsList(ArrayList<NewsItem> mNewsList) {
        this.mNewsList.addAll(mNewsList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mNewsList != null && mNewsList.size() > 0) {
            return mNewsList.get(position).getPictureShowType();
        } else {
            return super.getItemViewType(position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                View smallImgView = LayoutInflater.from(mContext).inflate(R.layout.item_new_type_small_img, parent, false);
                return new SmallImgHolder(smallImgView);
            case 1:
                View multiImgView = LayoutInflater.from(mContext).inflate(R.layout.item_new_type_multi_img, parent, false);
                return new MultiImgHolder(multiImgView);
            case 2:
                View bigImgView = LayoutInflater.from(mContext).inflate(R.layout.item_new_type_big_img, parent, false);
                return new BigImgHolder(bigImgView);
            default:
        }
        View smallImgView = LayoutInflater.from(mContext).inflate(R.layout.item_new_type_small_img, parent, false);
        return new SmallImgHolder(smallImgView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof MultiImgHolder) {
            MultiImgHolder multiImgHolder = (MultiImgHolder) holder;
            multiImgHolder.tvMultiNewTitle.setText(mNewsList.get(position).getTitle());
            multiImgHolder.tvMultiReadingVolume.setText(String.format("%s  %s  %s  %s", mNewsList.get(position).getWatchNum(), mContext.getString(R.string.peading_number), getPublishTime(mNewsList.get(position).getCreateTime()), mContext.getString(R.string.release)));
            multiImgHolder.setUtls(mNewsList.get(position).getThumbnail());
        } else if (holder instanceof BigImgHolder) {
            BigImgHolder bigImgHolder = (BigImgHolder) holder;
            bigImgHolder.tvBigNewTitle.setText(mNewsList.get(position).getTitle());
            bigImgHolder.tvBigReadingVolume.setText(String.format("%s  %s  %s  %s", mNewsList.get(position).getWatchNum(), mContext.getString(R.string.peading_number), getPublishTime(mNewsList.get(position).getCreateTime()), mContext.getString(R.string.release)));
            OdinGlideModule.loadWithRoundedCorners(mContext, getImgUrl(mNewsList.get(position).getThumbnail()), bigImgHolder.ivBigNewCover, R.drawable.shape_default_cover);
        } else if (holder instanceof SmallImgHolder) {
            SmallImgHolder smallImgHolder = (SmallImgHolder) holder;
            smallImgHolder.tvSmallNewTitle.setText(mNewsList.get(position).getTitle());
            smallImgHolder.tvSmallReadingVolume.setText(String.format("%s  %s  %s  %s", mNewsList.get(position).getWatchNum(), mContext.getString(R.string.peading_number), getPublishTime(mNewsList.get(position).getCreateTime()), mContext.getString(R.string.release)));
            OdinGlideModule.loadWithRoundedCorners(mContext, getImgUrl(mNewsList.get(position).getThumbnail()), smallImgHolder.ivSmallNewCover, R.drawable.shape_default_cover);
        }

        holder.itemView.setOnClickListener(v -> {
            String url = mNewsList.get(position).getUrl();
            int commentStatus = mNewsList.get(position).getCommentStatus();
            String title = mNewsList.get(position).getTitle();
            int commentNum = mNewsList.get(position).getCommentNum();
            int id = mNewsList.get(position).getNewId();
            if (url.isEmpty()) {
                return;
            }
            if (UserDataKt.getIsLogin()) {
                ReadArticleActivity.Companion.startReadArticleActivity(mContext, url, commentStatus != 0, title, String.valueOf(id), commentNum);
                return;
            }

            if (!UserDataKt.getIsLogin()) {
                ReadArticleActivity.Companion.startReadArticleActivity(mContext, url, false, title, String.valueOf(id), commentNum);
            }
        });
    }

    private String getPublishTime(String createTime) {
        return TimeUtil.getfriendlyTime(TimeUtil.getDateByFormat(createTime, "yyyy-MM-dd HH:mm:ss").getTime());
    }

    private String getImgUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }
        if (url.contains(",")) {
            return url.split(",")[0];
        }
        return url;
    }


    @Override
    public int getItemCount() {
        return mNewsList == null ? 0 : mNewsList.size();
    }


    private class BigImgHolder extends RecyclerView.ViewHolder {
        ImageView ivBigNewCover;
        TextView tvBigNewTitle;
        TextView tvBigReadingVolume;

        BigImgHolder(View itemView) {
            super(itemView);
            tvBigNewTitle = itemView.findViewById(R.id.tvBigImgNewTitle);
            tvBigReadingVolume = itemView.findViewById(R.id.tvBigImgReadingVolume);
            ivBigNewCover = itemView.findViewById(R.id.ivBigImgNewCover);
        }
    }

    private class SmallImgHolder extends RecyclerView.ViewHolder {
        ImageView ivSmallNewCover;
        TextView tvSmallNewTitle;
        TextView tvSmallReadingVolume;

        SmallImgHolder(View itemView) {
            super(itemView);
            tvSmallNewTitle = itemView.findViewById(R.id.tvSmallImgNewTitle);
            tvSmallReadingVolume = itemView.findViewById(R.id.tvSmallImgReadingVolume);
            ivSmallNewCover = itemView.findViewById(R.id.ivSmallImgNewCover);
        }
    }

    private class MultiImgHolder extends RecyclerView.ViewHolder {
        RecyclerView rvMultiThumbnailList;
        TextView tvMultiNewTitle;
        TextView tvMultiReadingVolume;

        MultiImgHolder(View itemView) {
            super(itemView);
            tvMultiNewTitle = itemView.findViewById(R.id.tvMultiImgNewTitle);
            tvMultiReadingVolume = itemView.findViewById(R.id.tvMultiImgReadingVolume);
            rvMultiThumbnailList = itemView.findViewById(R.id.rvMultiImgThumbnailList);
            rvMultiThumbnailList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        }

        void setUtls(String url) {
            MultiCoverAdapter multiCoverAdapter = new MultiCoverAdapter(mContext, url);
            rvMultiThumbnailList.setAdapter(multiCoverAdapter);
        }
    }
}
